from django.db import models

# Create your models here.
from dvadmin.system.models import Users
from dvadmin.utils.models import CoreModel


class EnvInfo(CoreModel):
    env_name = models.CharField(verbose_name='环境名称', max_length=40, null=False, unique=True)
    base_url = models.CharField(verbose_name='请求地址', max_length=40, null=False)
    paramsbody = models.JSONField(verbose_name='环境参数', max_length=200, null=True,blank=True)
    default = models.BooleanField(verbose_name='是否默认', default=False)
    class Meta:
        verbose_name='环境管理'
        db_table = 'api_envinfo'

class ProjectInfo(CoreModel):
    class Meta:
        verbose_name = '项目信息'
        db_table = 'api_projectinfo'

    project_name = models.CharField(verbose_name='项目名称', max_length=50, unique=True, null=False)
    responsible_user = models.ManyToManyField(to=Users, related_name='user1',null=True, blank=True, db_constraint=False,
                                    verbose_name="负责人")
    dev_user = models.ManyToManyField(to=Users,verbose_name='开发人员', related_name='user3',max_length=100, null=True,blank=True,db_constraint=False)
    publish_app = models.CharField(verbose_name='发布应用', max_length=100, null=False)


class ModuleInfo(CoreModel):
    class Meta:
        verbose_name = '模块信息'
        db_table = 'api_moduleinfo'

    module_name = models.CharField(verbose_name='模块名称', max_length=50, null=False,help_text="关联项目")
    project_belong = models.ForeignKey(
        to="ProjectInfo",
        verbose_name="所属项目",
        on_delete=models.CASCADE,
        db_constraint=False,
        help_text="关联项目",
    )
    test_user = models.ManyToManyField(to=Users, verbose_name='测试人员', related_name='user2', max_length=100, null=True,
                                       blank=True, db_constraint=False)

class TestApiInfo(CoreModel):
    class Meta:
        verbose_name = '接口信息'
        db_table = 'api_testinfo'
        ordering = ("project_belong",)

    name = models.CharField(verbose_name='接口名称', max_length=50, null=False)
    project_belong = models.ForeignKey(to="ProjectInfo", verbose_name='所属项目', on_delete=models.CASCADE, max_length=50,
                                       null=False,help_text='所属项目')
    module_belong = models.ForeignKey(verbose_name="所属模块",to="ModuleInfo", on_delete=models.CASCADE,help_text='所属模块')
    url= models.CharField(verbose_name='URL', max_length=200, null=False)
    METHOD_TYPE = (
        (0, "GET"),
        (1, "POST"),
        (2, "PUT"),
        (3, "DELETED"),
    )
    method = models.IntegerField(choices=METHOD_TYPE,verbose_name='Method', default=0,null=False,help_text='Method')
    PARAMS_TYPE = [
        (0, "header"),
        (1, "parmas"),
        (2, "json"),
    ]
    params_type = models.IntegerField(choices=PARAMS_TYPE, default=0, verbose_name='参数类型', null=False,help_text='参数类型')
    headers = models.JSONField(verbose_name='头部信息',max_length=200,null=True,blank=True)
    params = models.CharField(verbose_name='普通参数', max_length=200,null=True,blank=True)
    jsonbody = models.JSONField(verbose_name='json参数', max_length=200,null=True,blank=True)
    verification = models.CharField(verbose_name='检查点', max_length=200, null=True,blank=True)
    extract =  models.CharField(verbose_name='关联', max_length=200, null=True,blank=True)



class TestCase(CoreModel):
    class Meta:
        verbose_name = '用例集合'
        db_table = 'api_testcase'

    case_name = models.CharField(verbose_name='用例名称',max_length=100, null=False,unique=True)
    project_belong = models.ForeignKey(to="ProjectInfo", verbose_name='所属项目', on_delete=models.CASCADE, max_length=50,
                                 null=False)
    module_belong = models.ForeignKey(to="ModuleInfo",verbose_name="所属模块", on_delete=models.CASCADE)
    apis = models.ManyToManyField(to="TestApiInfo",verbose_name="包含接口", db_constraint=False,null=False)
    cron = models.CharField(verbose_name='定时时间',max_length=100, null=True,blank=True)
    task_id = models.CharField(verbose_name="所属任务", max_length=100, null=True, blank=True)


class TestApiResult(CoreModel):
    project_belong = models.ForeignKey(to="ProjectInfo", verbose_name='所属项目', on_delete=models.CASCADE, max_length=50,
                                       null=False, help_text='所属项目')
    module_belong = models.ForeignKey(verbose_name="所属模块", to="ModuleInfo", on_delete=models.CASCADE, help_text='所属模块')
    api_belong=models.ForeignKey(verbose_name="所属接口", to="TestApiInfo", on_delete=models.CASCADE, help_text='所属接口')
    took_time = models.IntegerField(verbose_name='话费时间', null=True,blank=True,help_text='时间')
    result=models.CharField(verbose_name='返回结果', max_length=200, null=True,blank=True,help_text='返回结果')
    verification=models.CharField(verbose_name='校验结果', max_length=200, null=True,blank=True,help_text='校验结果')

    env_belong = models.ForeignKey(verbose_name="所属环境", to="EnvInfo", on_delete=models.CASCADE, help_text='所属环境',
                                   null=True, blank=True)
    case_belong = models.ForeignKey(verbose_name="所属用例", to="TestCase", on_delete=models.CASCADE, help_text='所属案例',
                                   null=True, blank=True)
    class Meta:
        db_table = 'api_result'